package com.android.powerbankpad.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.android.powerbankpad.LauncherActivity;
import com.android.powerbankpad.config.Constants;
import com.ws.universal.tools.eventbus.BaseBusData;
import com.ws.universal.tools.eventbus.EventBusUtil;
import com.ws.universal.tools.utils.AppUtils;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.utils.LogcatUtils;
import com.ws.universal.tools.utils.ShellUtils;
import com.ws.universal.tools.utils.StringUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class BootReceiver extends BroadcastReceiver {
    private BaseBusData baseBusData;


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String packageName = intent.getDataString();
        LogcatUtils.e(" BootReceiver onReceive(): getAction = " + action + " , 包名 = " + packageName);

        //中断广播
        if (isOrderedBroadcast()) {
            setResultCode(Activity.RESULT_OK);
        }
        if (StringUtils.isEmpty(action)) {
            return;
        }
        if (!AppUtils.isAppForeground()) {
            AppUtils.setTopApp();
        }

        switch (action) {


            case Constants.BROADCAST_ACTION://事件广播
                //2:联网成功,4:租借成功,5:租借失败,6:归还成功,7:归还失败
                int voice = intent.getIntExtra("voice", -1);
                String id = intent.getStringExtra("id");
                LogcatUtils.e(" BootReceiver onReceive(): id :" + id + " voice " + voice);
                //2:联网成功,4:租借功,5:租借失败,6:归还成功,7:归还失败
                if (voice > 0) {
                    baseBusData = new BaseBusData(String.valueOf(voice));
                    EventBusUtil.postEvent(baseBusData);
                }
                if (!StringUtils.isEmpty(id) && voice == -1) {
                    CacheUtils.setString(Constants.serial_number, id);
                    baseBusData = new BaseBusData(Constants.SET_SERNUM_BROADCAST_ACTION);
                    EventBusUtil.postEvent(baseBusData);
                }

                break;

            case Constants.SET_SERNUM_BROADCAST_ACTION://设备编码
                String serialNumber = intent.getStringExtra("id");
                LogcatUtils.e(" BootReceiver onReceive(): 序列号 :" + serialNumber);
                if (!StringUtils.isEmpty(serialNumber)) {
                    CacheUtils.setString(Constants.serial_number, serialNumber);
                    baseBusData = new BaseBusData(Constants.SET_SERNUM_BROADCAST_ACTION);
                    EventBusUtil.postEvent(baseBusData);
                } else {
                    baseBusData = new BaseBusData(Constants.serial_number_null);
                    EventBusUtil.postEvent(baseBusData);

                }
                break;


            case Intent.ACTION_PACKAGE_REPLACED:
                // 重新启动APP
                //接收更新广播
                LogcatUtils.e(" BootReceiver onReceive():更新了 开始启动 ");

                Observable
                        .timer(5, TimeUnit.SECONDS)
                        .subscribe(new Observer<Long>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(Long aLong) {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                                if ("package:com.lechang.service2".equals(packageName)) {
                                    ShellUtils.execCmd("reboot");
                                } else if ("package:com.android.powerbankpad".equals(packageName)) {
                                    Intent intent3 = new Intent(context, LauncherActivity.class);
                                    intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    context.startActivity(intent3);
                                }

                            }
                        });

                break;


        }
    }
}
